package com.david.common.lang;

import lombok.Data;

import java.io.Serializable;

/**
 * @program: Blog-Vue
 * @description:统一封装的结果
 * @author: David
 * @version: Serializable实现序列化
 * @create: 2022-05-09 18:28
 **/
@Data
public class Result implements Serializable {

    /**
     * code 200表示正常,非200表示异常
     * msg 异常消息
     * data 数据
     */
    private int code;
    private String msg;
    private Object data;

    /**
     * success 成功
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Result success(int code, String msg, Object data){
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
    public static Result success(Object data){
        return success(200,"操作成功",data);
    }

    /**
     * fail 数据异常
     * @param data
     * @return
     */
    public static Result fail(String msg,Object data){
        return fail(400,msg,data);
    }
    public static Result fail(String msg){
        return fail(400,msg,null);
    }
    public static Result fail(int code, String msg, Object data){
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

}
